//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Manufacturer manager
    /// </summary>
    public class ManufacturerManager
    {
        #region Constants
        private const string MANUFACTURERS_ALL_KEY = "Nop.manufacturer.all-{0}";
        private const string MANUFACTURERS_BY_ID_KEY = "Nop.manufacturer.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Marks a manufacturer as deleted
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifer</param>
        public static void MarkManufacturerAsDeleted(int ManufacturerID)
        {
            Manufacturer manufacturer = GetByManufacturerID(ManufacturerID);
            if (manufacturer != null)
            {
                UpdateManufacturer(manufacturer.ManufacturerID, manufacturer.Name, manufacturer.Description,
                    manufacturer.TemplateID, manufacturer.MetaKeywords,
                    manufacturer.MetaDescription, manufacturer.MetaTitle,
                    manufacturer.SEName, manufacturer.PictureID, manufacturer.Published,
                    true, manufacturer.DisplayOrder, manufacturer.CreatedOn, manufacturer.UpdatedOn);
            }

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(MANUFACTURERS_ALL_KEY);
                NopCache.RemoveByPattern(MANUFACTURERS_BY_ID_KEY);
            }
        }
        /// <summary>
        /// Removes a manufacturer picture
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifier</param>

        public static void RemoveManufacturerPicture(int ManufacturerID)
        {
            Manufacturer manufacturer = GetByManufacturerID(ManufacturerID);
            if (manufacturer != null)
            {
                UpdateManufacturer(manufacturer.ManufacturerID, manufacturer.Name, manufacturer.Description,
                    manufacturer.TemplateID, manufacturer.MetaKeywords,
                    manufacturer.MetaDescription, manufacturer.MetaTitle,
                    manufacturer.SEName, 0, manufacturer.Published,
                    manufacturer.Deleted, manufacturer.DisplayOrder, manufacturer.CreatedOn, manufacturer.UpdatedOn);
            }

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(MANUFACTURERS_ALL_KEY);
                NopCache.RemoveByPattern(MANUFACTURERS_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets all manufacturers
        /// </summary>
        /// <returns>Manufacturer collection</returns>
        public static ManufacturerCollection GetAllManufacturers()
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(MANUFACTURERS_ALL_KEY, showHidden);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ManufacturerCollection)obj2;
            }

            ManufacturerCollection manufacturerCollection = ManufacturerDBManager.GetAllManufacturers(showHidden);
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, manufacturerCollection);
            }
            return manufacturerCollection;
        }

        /// <summary>
        /// Gets a manufacturer
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <returns>Manufacturer</returns>
        public static Manufacturer GetByManufacturerID(int ManufacturerID)
        {
            string key = string.Format(MANUFACTURERS_BY_ID_KEY, ManufacturerID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (Manufacturer)obj2;
            }

            Manufacturer manufacturer = ManufacturerDBManager.GetByManufacturerID(ManufacturerID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, manufacturer);
            }
            return manufacturer;
        }

        /// <summary>
        /// Inserts a manufacturer
        /// </summary>
        /// <param name="Name">The name</param>
        /// <param name="Description">The description</param>
        /// <param name="TemplateID">The template identifier</param>
        /// <param name="MetaKeywords">The meta keywords</param>
        /// <param name="MetaDescription">The meta description</param>
        /// <param name="MetaTitle">The meta title</param>
        /// <param name="SEName">The search-engine name</param>
        /// <param name="PictureID">The parent picture identifier</param>
        /// <param name="Published">A value indicating whether the entity is published</param>
        /// <param name="Deleted">A value indicating whether the entity has been deleted</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Manufacturer</returns>
        public static Manufacturer InsertManufacturer(string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Manufacturer manufacturer = ManufacturerDBManager.InsertManufacturer(Name, Description,
             TemplateID, MetaKeywords, MetaDescription, MetaTitle,
             SEName, PictureID, Published, Deleted,
             DisplayOrder, CreatedOn, UpdatedOn);

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(MANUFACTURERS_ALL_KEY);
                NopCache.RemoveByPattern(MANUFACTURERS_BY_ID_KEY);
            }

            return manufacturer;
        }

        /// <summary>
        /// Updates the manufacturer
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Description">The description</param>
        /// <param name="TemplateID">The template identifier</param>
        /// <param name="MetaKeywords">The meta keywords</param>
        /// <param name="MetaDescription">The meta description</param>
        /// <param name="MetaTitle">The meta title</param>
        /// <param name="SEName">The search-engine name</param>
        /// <param name="PictureID">The parent picture identifier</param>
        /// <param name="Published">A value indicating whether the entity is published</param>
        /// <param name="Deleted">A value indicating whether the entity has been deleted</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Manufacturer</returns>
        public static Manufacturer UpdateManufacturer(int ManufacturerID, string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Manufacturer manufacturer = ManufacturerDBManager.UpdateManufacturer(ManufacturerID, Name, Description,
             TemplateID, MetaKeywords, MetaDescription, MetaTitle,
             SEName, PictureID, Published, Deleted,
             DisplayOrder, CreatedOn, UpdatedOn);

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(MANUFACTURERS_ALL_KEY);
                NopCache.RemoveByPattern(MANUFACTURERS_BY_ID_KEY);
            }

            return manufacturer;
        }
        #endregion
    }
}
